package cn.csc.module.product.controller.admin.stocklock;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.product.controller.admin.stocklock.vo.*;
import cn.csc.module.product.convert.stocklock.SkuStockLockConvert;
import cn.csc.module.product.dal.dataobject.stocklock.SkuStockLockDO;
import cn.csc.module.product.service.stocklock.SkuStockLockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 库存锁定信息")
@RestController
@RequestMapping("/product/sku-stock-lock")
@Validated
public class SkuStockLockController {

    @Resource
    private SkuStockLockService skuStockLockService;

    @PostMapping("/create")
    @ApiOperation("创建库存锁定信息")
    @PreAuthorize("@ss.hasPermission('product:sku-stock-lock:create')")
    public CommonResult<BigInteger> createSkuStockLock(@Valid @RequestBody SkuStockLockCreateReqVO createReqVO) {
        return success(skuStockLockService.createSkuStockLock(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新库存锁定信息")
    @PreAuthorize("@ss.hasPermission('product:sku-stock-lock:update')")
    public CommonResult<Boolean> updateSkuStockLock(@Valid @RequestBody SkuStockLockUpdateReqVO updateReqVO) {
        skuStockLockService.updateSkuStockLock(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除库存锁定信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:sku-stock-lock:delete')")
    public CommonResult<Boolean> deleteSkuStockLock(@RequestParam("id") BigInteger id) {
        skuStockLockService.deleteSkuStockLock(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得库存锁定信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:sku-stock-lock:query')")
    public CommonResult<SkuStockLockRespVO> getSkuStockLock(@RequestParam("id") BigInteger id) {
        SkuStockLockDO skuStockLock = skuStockLockService.getSkuStockLock(id);
        return success(SkuStockLockConvert.INSTANCE.convert(skuStockLock));
    }

    @GetMapping("/list")
    @ApiOperation("获得库存锁定信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:sku-stock-lock:query')")
    public CommonResult<List<SkuStockLockRespVO>> getSkuStockLockList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<SkuStockLockDO> list = skuStockLockService.getSkuStockLockList(ids);
        return success(SkuStockLockConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得库存锁定信息分页")
    @PreAuthorize("@ss.hasPermission('product:sku-stock-lock:query')")
    public CommonResult<PageResult<SkuStockLockRespVO>> getSkuStockLockPage(@Valid SkuStockLockPageReqVO pageVO) {
        PageResult<SkuStockLockDO> pageResult = skuStockLockService.getSkuStockLockPage(pageVO);
        return success(SkuStockLockConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出库存锁定信息 Excel")
    @PreAuthorize("@ss.hasPermission('product:sku-stock-lock:export')")
    @OperateLog(type = EXPORT)
    public void exportSkuStockLockExcel(@Valid SkuStockLockExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SkuStockLockDO> list = skuStockLockService.getSkuStockLockList(exportReqVO);
        // 导出 Excel
        List<SkuStockLockExcelVO> datas = SkuStockLockConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "库存锁定信息.xls", "数据", SkuStockLockExcelVO.class, datas);
    }

}
